module.exports = {
	/**
	 * 修改数据
	 * @url admin/recharge/sys/update 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: 'ok'
		};
		// 业务逻辑开始-----------------------------------------------------------
		// 获取前端传过来的参数
		let {
			_id,
			tvinfo,
			nickname, //添加这条充值方案的nickname
			username, //添加这条充值方案的username
			user_id, //添加这条充值方案的账户id
			recharge_amount,
			bean,
			jiaobiao,
			recharge_text,
			subject,
			judge,
			give_bean, //多送多少豆
			percentage_user, //xx%的用户选择
			indulgence, //是否特惠充值
			sort,
			invite_code,
			appid,
			appname,
			tv_id,
			tv_name,
			_add_time,
			_add_time_str,
			zdytext,
			recharge_beizhu,
			days
		} = data;
		// 获取前端传过来的参数

		try {

			_id = tvinfo._id,

				recharge_amount = tvinfo.recharge_amount,

				bean = tvinfo.bean,

				recharge_text = tvinfo.recharge_text,

				subject = tvinfo.subject,

				days = tvinfo.days || 0,
				hour = tvinfo.hour || 0,

				judge = tvinfo.judge,

				jiaobiao = tvinfo.jiaobiao,

				invite_code = tvinfo.invite_code,

				appid = tvinfo.appid,

				appname = tvinfo.appname,

				sort = tvinfo.sort,

				give_bean = tvinfo.give_bean, //多送多少豆

				percentage_user = tvinfo.percentage_user, //xx%的用户选择

				indulgence = tvinfo.indulgence, //是否特惠充值

				_add_time = tvinfo._add_time,

				_add_time_str = tvinfo._add_time_str,

				tv_id = tvinfo.tv_id,

				tv_name = tvinfo.tv_name,

				showFinger = tvinfo.showFinger,
				zdytext = tvinfo.zdytext,
				recharge_beizhu = tvinfo.recharge_beizhu

		} catch (err) {



		}
		// 参数验证开始
		if (vk.pubfn.isNull(_id)) return {
			code: -1,
			msg: 'id不能为空'
		};

		console.log("user=", user_id);
		//如果修改时该方案不存在user_id，则取当前用户信息
		if (!user_id) {
			nickname = userInfo.nickname || "nickname";
			username = userInfo.username || "username";
			user_id = userInfo._id || "user_id";
		}



		// 参数验证结束
		let dbName = "b_recharge"; // 表名
		// 执行 数据库 updateById 命令
		await vk.baseDao.updateById({
			dbName,
			id: _id,
			dataJson: {
				nickname, //添加这条充值方案的nickname
				username, //添加这条充值方案的username
				user_id, //添加这条充值方案的账户id
				recharge_amount,
				bean,
				jiaobiao,
				recharge_text,
				subject,
				days: days,
				hour,
				judge,
				give_bean, //多送多少豆
				percentage_user, //xx%的用户选择
				indulgence, //是否特惠充值
				sort,
				invite_code,
				appid,
				appname,
				tv_id,
				tv_name,
				_add_time,
				zdytext,
				recharge_beizhu,
				_add_time_str
			}
		});
		//以下代码是更新a_dy_tfurl表里面的充值方案数据,确保管理员/代理商修改充值方案后，用户侧可以及时更新

		//=====更新首充方案=====
		let update_bind_first_recharge = await db.collection('a_dy_tfurl').where({
			'bind_first_recharge._id': _id
		}).update({
			'bind_first_recharge.$.subject': subject,
			'bind_first_recharge.$.recharge_amount': recharge_amount,
			'bind_first_recharge.$.bean': bean,
			'bind_first_recharge.$.judge': judge,
			'bind_first_recharge.$.percentage_user': percentage_user,
			'bind_first_recharge.$.give_bean': give_bean,
			'bind_first_recharge.$.recharge_text': recharge_text,
			'bind_first_recharge.$.sort': sort,
			'bind_first_recharge.$.appid': appid,
			'bind_first_recharge.$.appname': appname,
			'bind_first_recharge.$.indulgence': indulgence,
			'bind_first_recharge.$.jiaobiao': jiaobiao,

			'bind_first_recharge.$.days': days,
		})


		//=====更新复充方案=====
		let update_bind_again_recharge = await db.collection('a_dy_tfurl').where({
			'bind_again_recharge._id': _id
		}).update({
			'bind_again_recharge.$.subject': subject,
			'bind_again_recharge.$.recharge_amount': recharge_amount,
			'bind_again_recharge.$.bean': bean,
			'bind_again_recharge.$.judge': judge,
			'bind_again_recharge.$.percentage_user': percentage_user,
			'bind_again_recharge.$.give_bean': give_bean,
			'bind_again_recharge.$.recharge_text': recharge_text,
			'bind_again_recharge.$.sort': sort,
			'bind_again_recharge.$.appid': appid,
			'bind_again_recharge.$.appname': appname,
			'bind_again_recharge.$.indulgence': indulgence,
			'bind_again_recharge.$.jiaobiao': jiaobiao,
			'bind_again_recharge.$.days': days,

		})
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}